#include"rotor.h"

Rotor::Rotor(int e):QFrame()
{
    /*
      Création des instances composants la main window
      */
    encoche = e;



    label_top = new QLabel();
    separ_top_left = new QFrame();
    separ_top_left->setFrameShape(QFrame::NoFrame);

    /*
    separ_top_right = new QFrame();
    separ_top_right->setFrameShape(QFrame::HLine);
*/


    label_mid = new QLabel();
    separ_mid_left = new QFrame();
    separ_mid_left->setFrameShape(QFrame::NoFrame);


    separ_mid_right = new QFrame();
    separ_mid_right->setFrameShape(QFrame::NoFrame);


    label_bot = new QLabel();
    separ_bot_left = new QFrame();
    separ_bot_left->setFrameShape(QFrame::NoFrame);

    /*
    separ_bot_right = new QFrame();
    separ_bot_right->setFrameShape(QFrame::HLine);
*/

    layout_rotor = new QGridLayout();
    choix = new QComboBox();
    QString s;
    for(char c = 'A'; c <= 'Z'; ++c)
    {
        s = c;
        choix->addItem(s);
    }


    /*
      Partie layout
      */
    layout_rotor->addWidget(choix,1,1);

    layout_rotor->addWidget(separ_top_left,0,0);
 //   layout_rotor->addWidget(separ_top_right,0,2);

    layout_rotor->addWidget(separ_mid_left,1,0);
    layout_rotor->addWidget(separ_mid_right,1,2);

    layout_rotor->addWidget(separ_bot_left,2,0);
 //   layout_rotor->addWidget(separ_bot_right,2,2);



    layout_rotor->setSpacing(0);

    this->setLayout(layout_rotor);

}


void Rotor::phase2_rotor()
{

    label_mid->show();
    label_top->show();
    label_bot->show();
    choix->hide();
    QFont f ("Arial",13);
    QString init_value = choix->currentText();
    separ_mid_right->setFrameShape(QFrame::HLine);
    set_rotor(init_value);

}


void Rotor::raz_rotor()
{

    label_mid->hide();
    label_top->hide();
    label_bot->hide();
    separ_mid_right->setFrameShape(QFrame::NoFrame);

    choix->show();
}

/*
  Retourne l'entier correspondant à la position (nécessaire pour afficher l'encoche)
  */
void Rotor::set_rotor(QString init_value)
{
    QFont f ("Arial",13);

    label_mid->setText(init_value);
    label_mid->setFont(f);
    label_mid->setStyleSheet("QLabel { background-color : white; color : red; }");
    label_mid->setAlignment(Qt::AlignCenter);
    layout_rotor->addWidget(label_mid,1,1);

    label_top->setFont(f);
    label_top->setStyleSheet("QLabel { background-color : grey; color : white; }");
    label_top->setAlignment(Qt::AlignCenter);
    layout_rotor->addWidget(label_top,0,1);


    label_bot->setFont(f);
    label_bot->setStyleSheet("QLabel { background-color : grey; color : white; }");
    label_bot->setAlignment(Qt::AlignCenter);
    layout_rotor->addWidget(label_bot,2,1);

    int res = -1;

    if(init_value.compare("A") == 0)
    {
        label_top->setText("B");

        label_bot->setText("Z");
        res = 0;
    }
    if(init_value.compare("B") == 0)
    {
        label_top->setText("C");


        label_bot->setText("A");

        res = 1;
    }
    if(init_value.compare("C") == 0)
    {
        label_top->setText("D");


        label_bot->setText("B");

        res = 2;
    }
    if(init_value.compare("D") == 0)
    {
        label_top->setText("E");


        label_bot->setText("C");

        res = 3;
    }
    if(init_value.compare("E") == 0)
    {
        label_top->setText("F");


        label_bot->setText("D");

        res = 4;
    }
    if(init_value.compare("F") == 0)
    {
        label_top->setText("G");


        label_bot->setText("E");

        res = 5;
    }
    if(init_value.compare("G") == 0)
    {
        label_top->setText("H");


        label_bot->setText("F");

        res = 6;
    }
    if(init_value.compare("H") == 0)
    {
        label_top->setText("I");


        label_bot->setText("G");

        res = 7;
    }
    if(init_value.compare("I") == 0)
    {
        label_top->setText("J");


        label_bot->setText("H");

        res = 8;
    }
    if(init_value.compare("J") == 0)
    {
        label_top->setText("K");

        label_bot->setText("I");

        res = 9;
    }
    if(init_value.compare("K") == 0)
    {

        label_top->setText("L");


        label_bot->setText("J");

        res = 10;
    }
    if(init_value.compare("L") == 0)
    {
        label_top->setText("M");


        label_bot->setText("K");

        res = 11;
    }
    if(init_value.compare("M") == 0)
    {
        label_top->setText("N");


        label_bot->setText("L");

        res = 12;
    }
    if(init_value.compare("N") == 0)
    {
        label_top->setText("O");

        label_bot->setText("M");

        res = 13;
    }
    if(init_value.compare("O") == 0)
    {
        label_top->setText("P");

        label_bot->setText("N");

        res = 14;
    }
    if(init_value.compare("P") == 0)
    {
        label_top->setText("Q");

        label_bot->setText("O");

        res = 15;
    }
    if(init_value.compare("Q") == 0)
    {
        label_top->setText("R");

        label_bot->setText("P");

        res = 16;
    }
    if(init_value.compare("R") == 0)
    {
        label_top->setText("S");

        label_bot->setText("Q");

        res = 17;
    }
    if(init_value.compare("S") == 0)
    {
        label_top->setText("T");

        label_bot->setText("R");

        res = 18;
    }
    if(init_value.compare("T") == 0)
    {
        label_top->setText("U");


        label_bot->setText("S");

        res = 19;
    }
    if(init_value.compare("U") == 0)
    {
        label_top->setText("V");


        label_bot->setText("T");

        res = 20;
    }
    if(init_value.compare("V") == 0)
    {
        label_top->setText("W");


        label_bot->setText("U");

        res = 21;
    }
    if(init_value.compare("W") == 0)
    {
        label_top->setText("X");


        label_bot->setText("V");

        res = 22;
    }
    if(init_value.compare("X") == 0)
    {
        label_top->setText("Y");


        label_bot->setText("W");

        res = 23;
    }
    if(init_value.compare("Y") == 0)
    {
        label_top->setText("Z");


        label_bot->setText("X");

        res = 24;
    }
    if(init_value.compare("Z") == 0)
    {
        label_top->setText("A");


        label_bot->setText("Y");

        res = 25;
    }

    if(res == (encoche - 1))
    {
        separ_top_left->setFrameShape(QFrame::HLine);

    }
    else
    {
        separ_top_left->setFrameShape(QFrame::NoFrame);

    }
    if(res == (encoche))
    {
        separ_mid_left->setFrameShape(QFrame::HLine);
    }
    else
    {
        separ_mid_left->setFrameShape(QFrame::NoFrame);
    }
    if(res == (encoche + 1))
    {
        separ_bot_left->setFrameShape(QFrame::HLine);
    }
    else
    {
        separ_bot_left->setFrameShape(QFrame::NoFrame);
    }

}


QLabel* Rotor::getLabelMid()
{
    return label_mid;
}

